package com.cloud.seatatmone.service.impl;

import com.cloud.seatatmone.dao.TblOneMapper;
import com.cloud.seatatmone.entity.TblOne;
import com.cloud.seatatmone.service.Seata_TCC_Service;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

@Service
public class Seata_TCC_ServiceImpl implements Seata_TCC_Service {

    @Autowired
    private TblOneMapper oneMapper;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    @Transactional
    public String test_TCC(BusinessActionContext businessActionContext) {
        TblOne one = TblOne.Build().name("Seata的TCC模式测试").build();
        oneMapper.insertTblOne(one);

        restTemplate.postForEntity("http://seata-rm-two/test_TCC",null,null);
//        restTemplate.postForEntity("http://seata-rm-three/test_TCC",null,null);

        return "提交成功";
    }

    @Override
    @Transactional
    public boolean commitTCC(BusinessActionContext businessActionContext) {
        System.out.println("提交成功");
        return true;
    }

    @Override
    @Transactional
    public boolean rollbackTCC(BusinessActionContext businessActionContext) {
        TblOne one = TblOne.Build().name("Seata的TCC模式测试").build();
        oneMapper.deleteByName(one);
        System.out.println("回滚成功");
        return true;
    }
}
